package com.hospital.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hospital.yygh.hosp.repository.DepartmentRepository;
import com.hospital.yygh.hosp.service.DepartmentService;
import com.hospital.yygh.model.hosp.Department;
import com.hospital.yygh.vo.hosp.DepartmentQueryVo;
import com.hospital.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 上传科室接口
     *
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        // paramMap转换成Department对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department =
                JSONObject.parseObject(paramMapString, Department.class);

        // 根据医院编号和科室信息进行查询,取名采取spring DATA规范
        Department departmentExist = departmentRepository
                .getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        // 判断科室是否存在
        if (departmentExist != null) {
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }


    }

    /**
     * 查询科室接口
     *
     * @param page
     * @param limit
     * @param departmentQueryVo
     */
    @Override
    public Page<Department> finPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // MongoRepository开发CRUD
        // 当前页从0开始，数据库是从1开始的
        PageRequest pageable = PageRequest.of(page - 1, limit);
        // 将departmentQueryVo对象转换为department对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        // 创建Example对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)   // 模糊查询
                .withIgnoreCase(true);  // 忽略大小写
        Example<Department> example = Example.of(department, matcher);
        Page<Department> departmentPage = departmentRepository.findAll(example, pageable);
        return departmentPage;
    }


    /**
     * 删除科室接口
     *
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {
        // 根据医院编号和科室编号查询科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            // 删除
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 查询所有科室列表(根据医院编号，查询所有科室列表)
     *
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDepTree(String hoscode) {
        // 创建List集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        // 根据医院编号，查询所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        // 所有科室列表信息
        List<Department> departmentList = departmentRepository.findAll(example);

        // 根据大科室编号bigcode进行分组，获取每个大科室里面的下级子科室
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // 遍历map集合
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            // 大科室编号
            String bigCode = entry.getKey();
            // 大科室编号对应的全部数据
            List<Department> departmentList1 = entry.getValue();
            // 封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            departmentVo1.setDepname(departmentList1.get(0).getBigname());
            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : departmentList1) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                // 封装到List集合
                children.add(departmentVo2);
            }
            // 把小科室List集合放到大科室children里面
            departmentVo1.setChildren(children);
            // 放到最终result里面
            result.add(departmentVo1);
        }
        return result;
    }

    /**
     * 根据医院编号和科室编号查询科室名称
     *
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    /**
     * 根据医院编号和科室编号查询科室
     *
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
